Page({

     data:{

       code:""


     },
     Onload(){


   

     },
     //1.0提交表单
     formSubmit: function(e) {
    // console.log('form发生了submit事件，携带数据为：', e.detail.value);
      
      //1.0定义参数接收表单值
       var stid=e.detail.value.staffid;
       
       //获取全局变量钉钉的授权码
      // var app = getApp()
       // console.log(app.globalData) // 获取 globalData
         var that=this;
     //2.0获取临时授权码
       dd.getAuthCode({
        success:function(res){
        /*{
            authCode: 'hYLK98jkf0m' //string authCode
        }*/

        var passcode = res.authCode;
        console.log(passcode);
        
        //设置Data数据
        // that.setData({

        //   code:passcode
          

        // });
       //3.0 发起调用请求
        dd.httpRequest({
      
          //提交工号和授权码，如果授权码和工号对上了就返回姓名，然后保存工号到缓存
          //url: 'https://oapi.dingtalk.com/user/getuserinfo?access_token=' + access_token + '&code=' + code,
          url:'http://ding.tida-lighting.com/api/user/userbind?staffid='+stid+'&code='+passcode,
          success: function (res) {
            console.log(res.data);
           var iname=  res.data.fname;
           var uid=res.data.userid;
          if(iname!=null)
          {
           console.log("名字",iname);

            dd.setStorage({
                      key: 'uid',
                      data:uid,
                      success: function() {},});
           //that=this;
          //3.1储存工号信息到缓存
          dd.setStorage({
                      key: 'staffid',
                      data:stid,
                      success: function() {
   
                     dd.showToast({content: iname+'绑定工号'+stid+'成功'});
                          }
                      });
                      dd.setStorage({
                      key: 'name',
                      data:iname,
                      success: function() {
                     // dd.navigateTo({ url:'../first/first' });
                 
                      setTimeout(function () {
                           dd.navigateBack({
      delta: 3
     });
     //要延时执行的代码
    }, 2000);

                          }
                      });
                    

  
                      
            
          
          }else
          {

             dd.showToast({content: '无此号'+stid+'绑定失败，请重新绑定'});
             console.log(res);
             

          } },
        fail: (err) => {
          // dd.alert({content: JSON.stringify(err)})
           dd.showToast({content: name+'绑定工号'+stid+'失败，请重新绑定'});
        }

     });//发起结束

   

    },
    fail:function(err){
      dd.showToast({content:err});
    }
});
     },



       //获取全局变量
      //var app = getApp();



      //console.log("globaldata",app.globalData);
      //var code=app.globalData.passcode;
     
     
     
   formReset: function() {
    
       dd.getAuthCode({
        success:function(res){
        /*{
            authCode: 'hYLK98jkf0m' //string authCode
        }*/

        var passcode = res.authCode;
        console.log(passcode);
        }});

    dd.removeStorage({
  key: 'uid',
  success: function(){
  
  }
});
                      
           dd.removeStorage({
  key: 'staffid',
  success: function(){

  }
});
 dd.removeStorage({
  key: 'name',
  success: function(){
  
  }
});
 

     }


         
 

  
});
